package com.xlongwei.accounts.dao;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.core.util.ClassUtil;
import org.noear.solon.data.annotation.Cache;
import org.noear.solon.data.annotation.CacheRemove;
import org.noear.solon.data.annotation.Tran;
import org.noear.solon.data.sql.SqlBuilder;
import com.xlongwei.accounts.config.MySqlUtils;
import com.xlongwei.accounts.model.Extra;

@Component
public class ExtraDao {
    @Inject
    MySqlUtils sqlUtils;
    static boolean hasEmojiParser = ClassUtil.loadClass("com.vdurmont.emoji.EmojiParser") != null;

    @Tran
    @CacheRemove(keys = "extra.${}")
    public Integer create(Extra extra) {
        assert extra.getRecordid() != null;
        try{
            return sqlUtils
                    .sql("insert into t_extra(recordid,remark) values(?,?)", extra.getRecordid(), extra.getRemark())
                    .update();
        } catch (Exception e) {
        }
        if(hasEmojiParser) {
            try{
                return sqlUtils
                        .sql("insert into t_extra(recordid,remark) values(?,?)", extra.getRecordid(), toAlias(extra.getRemark()))
                        .update();
            } catch (Exception e) {
            }
        }
        return 0;
    }

    @Cache(key = "extra.${recordid}")
    public Extra retrive(Integer recordid) {
        Extra extra = sqlUtils.sql("select recordid,remark from t_extra where recordid=?", recordid).queryRow(Extra.class);
        if (hasEmojiParser && extra != null) {
            extra.setRemark(toUnicode(extra.getRemark()));
        }
        return extra;
    }

    @Tran
    @CacheRemove(keys = "extra.${extra.recordid}")
    public Integer update(Extra extra) {
        assert extra.getRecordid() != null;
        try{
            return sqlUtils.sql("update t_extra set remark=? where recordid=?", extra.getRemark(), extra.getRecordid())
                    .update();
        } catch (Exception e) {
        }
        if(hasEmojiParser){
            try{
                return sqlUtils.sql("update t_extra set remark=? where recordid=?", toAlias(extra.getRemark()), extra.getRecordid())
                        .update();
            } catch (Exception e) {
            }
        }
        return 0;
    }

    public Map<Integer, Extra> list(List<Integer> recordids) {
        if (recordids != null && recordids.size() > 0) {
            SqlBuilder sqlSpec = new SqlBuilder();
            sqlSpec.append("select recordid,remark from t_extra where recordid in (?...)", recordids);
            List<Extra> list = sqlUtils.sql(sqlSpec).queryRowList(Extra.class);
            if (list != null && list.size() > 0) {
                return list.stream().collect(Collectors.toMap(Extra::getRecordid, Function.identity()));
            }
        }
        return Collections.emptyMap();
    }

    public static String toAlias(String remark) {
        try{
            return hasEmojiParser && remark != null ? com.vdurmont.emoji.EmojiParser.parseToAliases(remark) : remark;
        } catch (Exception e) {
            return remark;
        }
    }

    public static String toUnicode(String remark) {
        try{
            return hasEmojiParser && remark != null ? com.vdurmont.emoji.EmojiParser.parseToUnicode(remark) : remark;
        } catch (Exception e) {
            return remark;
        }
    }
}
